<template>
  <div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full space-y-8">
      <div>
        <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">注册</h2>
      </div>
      <form @submit.prevent="register" class="mt-8 space-y-6">
        <div class="rounded-md shadow-sm -space-y-px">
          <div class="mb-4">
            <label for="username" class="sr-only">用户名</label>
            <input v-model="username" id="username" name="username" type="text" autocomplete="username" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="用户名">
          </div>
          <div class="mb-4">
            <label for="email" class="sr-only">邮箱地址</label>
            <input v-model="email" id="email" name="email" type="email" autocomplete="email" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="邮箱地址">
          </div>
          <div class="mb-4">
            <label for="password" class="sr-only">密码</label>
            <input v-model="password" id="password" name="password" type="password" autocomplete="new-password" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="密码">
          </div>
          <div>
            <label for="confirm-password" class="sr-only">确认密码</label>
            <input v-model="confirmPassword" id="confirm-password" name="confirm-password" type="password" autocomplete="new-password" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-blue-500 focus:border-blue-500 focus:z-10 sm:text-sm" placeholder="确认密码">
          </div>
        </div>

        <div>
          <button type="submit" class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
            <span class="absolute left-0 inset-y-0 flex items-center pl-3">
              <i class="fa fa-user-plus text-blue-500 group-hover:text-blue-400"></i>
            </span>
            注册
          </button>
        </div>
        
        <div class="text-center">
          <p class="text-sm text-gray-600">
            已有账号？ <router-link to="/login" class="font-medium text-blue-600 hover:text-blue-500">登录</router-link>
          </p>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      email: '',
      password: '',
      confirmPassword: ''
    };
  },
  methods: {
    async register() {
      if (this.password !== this.confirmPassword) {
        return alert('两次输入的密码不一致');
      }
      
      try {
        await this.axios.post('/register', {
          username: this.username,
          password: this.password,
          email: this.email
        });
        
        alert('注册成功，请登录');
        this.$router.push({ name: 'Login' });
      } catch (error) {
        console.error(error);
        alert('注册失败，可能是用户名或邮箱已被使用');
      }
    }
  }
};
</script>  